// logs.js
const util = require('../../utils/util.js')

Page({
  data: {
    product_brand: '',
    product_allname: '',
    product_place: '',
    product_effect: '',
    product_net_content: '',
    logs: [],
    picArr: [],
    pic_id: [],
    hot:0,
    popular:0,
    snacks:0,
    beauty:0,
    product_introduce:'',
    service_name:[['','']]
  },
  plusName:function(){

    console.log('添加成功')
    var item = ['','']
    this.data.service_name.push(item)
    this.setData({
      service_name:this.data.service_name
    })
  },
  deleteName: function (e) {
    console.log(e)
    var that = this
    wx.showModal({
      title: '提示',
      content: '确认删除',
      success(res) {
        if (res.confirm) {
          console.log('用户点击确定')
          var tempIndex = e.target.dataset.id
          that.data.service_name.splice(tempIndex, 1)
          console.log(that.data.service_name)
          // console.log(that.data.service_name.join(''))

          // var conactArr = that.getNewArr()
          // console.log("名字",conactArr)
          that.setData({
            service_name: that.data.service_name
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  setPrice:function(e){
    var value = e.detail,value
    var id = e.target.dataset.id
    this.data.service_name[id][0] = value
    console.log(this.data.service_name)
    this.setData({
      service_name:this.data.service_name
    })
  },
  setNorms:function(e){
    console.log(e)
    var value = e.detail,value
    var id = e.target.dataset.id
    this.data.service_name[id][1] = value
    console.log(this.data.service_name)
    this.setData({
      service_name:this.data.service_name
    })
  },
  onLoad() {
    this.setData({
      logs: (wx.getStorageSync('logs') || []).map(log => {
        return util.formatTime(new Date(log))
      })
    })
  },
  ChooseImag: function (e) {
    var that = this
    wx.chooseImage({
      count: 1,
      sourceType: 'album',
      sizeType: ['compressed'],
      success(res) {
        that.setData({
          tempFilePaths: res.tempFilePaths[0]

        })
        setTimeout(function (e) {
          wx.showLoading({
            title: '正在上传图片...',
          })
        }, 300)
        let MyFile = new wx.BaaS.File()
        let fileParams = {
          filePath: that.data.tempFilePaths
        }
        let metaData = {
          categoryName: 'SDK',
          categoryName: 'image'
        }

        MyFile.upload(fileParams, metaData).then(res => {
          // 上传成功
          console.log(res)
          that.data.picArr.push(res.data.path)
          that.data.pic_id.push(res.data.file.id)
          that.setData({
            avatarUrl: res.data.path,
            picArr: that.data.picArr,
            pic_id: that.data.pic_id
          })
          wx.hideLoading()
          console.log(that.data.avatarUrl)
          // let tableName = 'service_items'
          // let recordID = wx.getStorageSync('id') // 数据行 id
          // let Product = new wx.BaaS.TableObject(tableName)
          // let product = Product.getWithoutData(recordID)

          // product.set('fukuanma', that.data.avatarUrl);
          // wx.setStorageSync('isInformation2', true)
          // product.update().then(res => {
          //     console.log(res)
          // }, err => {
          //     // err
          // })
        }, err => {
          // HError 对象
          // this.hideLoading()
          wx.showModal({
            title: '上传失败',
            content: '请等待三秒后重新点击<提交>',
            showCancel: false,
            success: res => {
              if (res.confirm) {
                that.setData({
                  submitDisabled: true
                })
                setTimeout(function () {
                  that.setData({
                    submitDisabled: false
                  })
                }, 5000)
              }
            }
          })
        })

      }
    })
  },
  submit: function () {
    var arr=this.data.service_name;
    var onearr=arr.reduce(function(a,b){
        return a.concat(b);
    })
    var items = []
    for(var i=0;i<onearr.length;i++){
      items.push(onearr[i].value)
    }
    console.log(onearr);
    // 向 tableName 为 'product' 的数据表插入一条记录
    let tableName = 'product'
    let Product = new wx.BaaS.TableObject(tableName)
    let product = Product.create()

    // 设置方式一
    let apple = {
      product_swiper_pic1: this.data.picArr[0],
      product_swiper_pic2: this.data.picArr[1],
      product_name: this.data.product_name,
      pre_cost: this.data.pre_cost,
      product_brand: this.data.product_brand,
      product_allname: this.data.product_allname,
      product_place: this.data.product_place,
      product_effect: this.data.product_effect,
      product_net_content: this.data.product_net_content,
      product_code:this.data.product_code,
      product_component:this.data.product_component,
      hot:this.data.hot,
      popular:this.data.popular,
      snacks:this.data.snacks,
      beauty:this.data.beauty,
      product_introduce:this.data.product_introduce,
      modal_cost_advise:items
    }
    console.log(apple)
    product.set(apple).save().then(res => {
      // success
      console.log(res)
    }, err => {
      //err 为 HError 对象
    })
  },
  deletePic: function (e) {

    console.log(e)
    var that = this
    wx.showModal({
      title: '提示',
      content: '确认删除',
      success(res) {
        if (res.confirm) {
          console.log('用户点击确定')
          var tempIndex = e.target.dataset.id
          if (that.data.picArr.length == 1) {
            that.setData({
              picArr: [],
              pic_id: []
            })
          } else {
            that.data.picArr.splice(tempIndex, 1)
            that.data.pic_id.splice(tempIndex, 1)

            var conactArr = that.getNewArr()
            var picArr = []
            picArr.push(conactArr[0])
            var pic_id = []
            pic_id.push(conactArr[1])
            console.log("名字", conactArr)
            console.log(picArr)
            that.setData({
              picArr: picArr,
              pic_id: pic_id
            })
            let MyFile = new wx.BaaS.File()
            MyFile.delete(that.data.pic_id[tempIndex]).then()
          }
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  getNewArr: function (e) {
    var conactArr = ['', '']
    for (var i = 0; i < this.data.picArr.length; i++) {
      conactArr[0] = conactArr[0].concat(this.data.picArr[i])
      conactArr[1] = conactArr[1].concat(this.data.pic_id[i])
    }
    return conactArr
  },
  setPINGPAI: function (e) {
    this.setData({
      product_brand: e.detail.value
    })
    console.log(this.data.product_brand)
  },
  setMINGCHEN: function (e) {
    this.setData({
      product_allname: e.detail.value
    })
    console.log(this.data.product_allname)
  },
  setCHANDI: function (e) {
    this.setData({
      product_place: e.detail.value
    })
    console.log(this.data.product_place)
  },
  setGONGXIAO: function (e) {
    this.setData({
      product_effect: e.detail.value
    })
    console.log(this.data.product_effect)
  },
  setJHL: function (e) {
    this.setData({
      product_net_content: e.detail.value
    })
    console.log(this.data.product_net_content)
  },
  productName: function (e) {
    this.setData({
      product_name: e.detail.value
    })
    console.log(this.data.product_name)
  },
  lastCost: function (e) {
    this.setData({
      pre_cost: e.detail.value
    })
    console.log(this.data.pre_cost)
  },
  setHotAndOther:function(e){
    console.log(e)
    if(e.detail.value==1)  this.data.hot = 1
    else if (e.detail.value==2) this.data.popular = 1
    else if (e.detail.value==3) this.data.snacks = 1
    else if(e.detail.value==4) this.data.beauty = 1
  },
  setIndex:function(e){
    console.log(e)
    if(e.detail.value==1)  this.data.product_introduce = '店长推荐'
    else if (e.detail.value==2) this.data.product_introduce = '爆款'
    else if (e.detail.value==3) this.data.product_introduce = '热卖'
  },
  setProductNum:function(e){
    this.setData({
      product_code: e.detail.value
    })
    console.log(this.data.product_code)
  },
  setRaw:function(e){
    this.setData({
      product_component: e.detail.value
    })
    console.log(this.data.product_component)
  }
})